package com.hck.infrastructure.persistent.repository;

import com.hck.domain.consultation.model.entity.HealthConsultationEntity;
import com.hck.domain.consultation.repository.IHealthConsultationRepository;
import com.hck.infrastructure.persistent.dao.IHealthConsultationDao;
import com.hck.infrastructure.persistent.po.HealthConsultation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * HealthConsultationRepository class description here.
 *
 * @author 黄朝看
 * @date 2025/6/30
 */
@Repository
public class HealthConsultationRepository implements IHealthConsultationRepository {
    @Resource
    private IHealthConsultationDao healthConsultationDao;

    @Override
    public void save(HealthConsultationEntity consultationEntity) {
        HealthConsultation healthConsultation = new HealthConsultation();
        BeanUtils.copyProperties(consultationEntity, healthConsultation);
        healthConsultationDao.insert(healthConsultation);
        consultationEntity.setId(healthConsultation.getId());
    }

    @Override
    public HealthConsultationEntity findById(Long id) {
        HealthConsultation healthConsultation = healthConsultationDao.selectById(id);
        if (healthConsultation == null) return null;
        HealthConsultationEntity entity = new HealthConsultationEntity();
        BeanUtils.copyProperties(healthConsultation, entity);
        return entity;
    }

    @Override
    public List<HealthConsultationEntity> findByUserId(Long userId) {
        return healthConsultationDao.selectByUserId(userId).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HealthConsultationEntity> findByDoctorId(Long doctorId) {
        return healthConsultationDao.selectByDoctorId(doctorId).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());
    }

    @Override
    public void updateStatus(Long id, String status, Date replyTime, Date closeTime) {
        healthConsultationDao.updateStatus(id, status, replyTime, closeTime);
    }
    
    @Override
    public Long countByUserId(Long userId) {
        return healthConsultationDao.countByUserId(userId);
    }
    
    @Override
    public List<HealthConsultationEntity> findByUserIdPage(Long userId, Integer offset, Integer limit) {
        return healthConsultationDao.selectByUserIdPage(userId, offset, limit).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());
    }
    
    @Override
    public Long countByDoctorId(Long doctorId) {
        return healthConsultationDao.countByDoctorId(doctorId);
    }
    
    @Override
    public List<HealthConsultationEntity> findByDoctorIdPage(Long doctorId, Integer offset, Integer limit) {
        return healthConsultationDao.selectByDoctorIdPage(doctorId, offset, limit).stream().map(po -> {
            HealthConsultationEntity entity = new HealthConsultationEntity();
            BeanUtils.copyProperties(po, entity);
            return entity;
        }).collect(Collectors.toList());
    }
}
